﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using DAL;
using Common;
using System.Data.Linq;
namespace BAL
{
    public class Diffic
    {
        private static string CacheName = "AllDiffic";
        /// <summary>
        /// 写入缓存
        /// </summary>
        /// <returns></returns>
        public static void ToCache()
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                CacheHelper.Refurbish(CacheName, db.Diffics.ToList(), 0, 60);
            }
        }
        public static int GetFirstId()
        {
            if (!CacheHelper.IsExist(CacheName))
            {
                ToCache();
            }
            IList<Models.Diffic> cache = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            return cache.First<Models.Diffic>().Id;
        }


        internal static DifficInfo TransDiffic2DifficInfo(Models.Diffic diffic)
        {
            DifficInfo difficInfo = new DifficInfo();

            Utility.SetObjectValueWithTheSamePropertyName(difficInfo, diffic);

            return difficInfo;
        }

        public static DifficInfo AddDiffic(DifficInfo difficInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                Models.Diffic diffic = new Models.Diffic();
                Utility.SetObjectValueWithTheSamePropertyName(diffic, difficInfo);
                DatabaseHelper.Insert<Models.Diffic>(diffic);
                ToCache();
                return TransDiffic2DifficInfo(diffic);
            }
        }

        public static void EditDiffic(DifficInfo difficInfo)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                try
                {
                    Models.Diffic diffic = db.Diffics.Single(b => b.Id == difficInfo.Id);

                    Utility.SetObjectValueWithTheSamePropertyName(diffic, difficInfo);

                    db.SubmitChanges(ConflictMode.ContinueOnConflict);
                    ToCache();
                }
                catch (ChangeConflictException)
                {
                    foreach (ObjectChangeConflict occ in db.ChangeConflicts)
                    {
                        occ.Resolve(RefreshMode.KeepCurrentValues);
                    }
                }
            }
        }
        public void Update(int id, int name)
        {

        }
        public void Delete(int id)
        {

        }
        public static void DelDiffic(int id)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                DelDiffic(new int[] { id });
            }
        }

        public static void DelDiffic(int[] lstId)
        {
            using (var db = DatabaseHelper.GetDashboardData())
            {
                if (lstId.Length > 0)
                {
                    foreach (int id in lstId)
                    {
                        Question.DelQuestionByDiffic(id);
                    }
                    string sql = string.Format("delete from Diffic where id in ({0})", string.Join(",", lstId.ConvertToStringArray()));
                    db.ExecuteCommand(sql);
                    ToCache();

                }
            }
        }

        public static int GetDifficIDByKeyword(string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            return Diffic_Info.Single(t => t.Keyword == keyword).Id;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Diffics.Single(t => t.Keyword == keyword).Id;
            //}
        }

        public static DifficInfo GetDifficInfoById(int Id)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            return TransDiffic2DifficInfo(Diffic_Info.Single(b => b.Id == Id));
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    Models.Diffic diffic = db.Diffics.Single(b => b.Id == Id);
            //    return TransDiffic2DifficInfo(diffic);
            //}
        }

        public static IList<DifficInfo> GetDifficInfoByIds(IList<int> Ids)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);

            return Diffic_Info.Where(p=>Ids.Contains(p.Id)).Select(c => TransDiffic2DifficInfo(c)).ToList();
        }
        public static IList<DifficInfo> GetDifficInfos(int first, int max)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            if (max == 0)
            {
                return Diffic_Info.Select(c => TransDiffic2DifficInfo(c)).ToList();
            }
            else
            {
                return Diffic_Info.Skip(first).Take(max).Select(c => TransDiffic2DifficInfo(c)).ToList();
            }
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    if (max == 0)
            //    {
            //        return db.GetTable<Models.Diffic>().Select(c => TransDiffic2DifficInfo(c)).ToList();
            //    }
            //    else
            //    {
            //        return db.GetTable<Models.Diffic>().Skip(first).Take(max).Select(c => TransDiffic2DifficInfo(c)).ToList();
            //    }
            //}
        }

        public static int GetDifficInfoCount()
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            return Diffic_Info.Count();
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.GetTable<Models.Diffic>().Count();
            //}
        }

        public static bool CheckDifficExistByKeyword(string keyword, int editid)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            return Diffic_Info.Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Diffics.Where(t => t.Keyword == keyword && t.Id != editid).Count() > 0;
            //}
        }

        public static bool CheckDifficExistByName(string name)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            return Diffic_Info.Where(w => w.Name == name).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Diffics.Where(w => w.Name == name).Count() > 0;
            //}
        }

        public static bool CheckDifficExistByName(string name, string keyword)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            return Diffic_Info.Where(w => w.Name == name || w.Keyword == keyword).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Diffics.Where(w => w.Name == name || w.Keyword == keyword).Count() > 0;
            //}
        }

        public static bool CheckDifficExistById(int Id)
        {
            if (!CacheHelper.IsExist(CacheName)) { ToCache(); }
            IList<Models.Diffic> Diffic_Info = (IList<Models.Diffic>)CacheHelper.GetCache(CacheName);
            return Diffic_Info.Where(w => w.Id == Id).Count() > 0;
            //using (var db = DatabaseHelper.GetDashboardData())
            //{
            //    return db.Diffics.Where(w => w.Id == Id).Count() > 0;
            //}
        }
    }
}
